<?php 

namespace App\Repositories;

use App\Services\PHPTree;
use Illuminate\Container\Container as App;
use App\Repositories\GroupRepository;

class MenuRepository extends Repository 
{
    public function __construct(App $app, GroupRepository $group) {
        parent::__construct($app); 
        $this->group = $group;
    }

    public function model() 
    {
        return 'App\Models\Menu';
    }

    // public function topMenu()
    // {
    // 	return $this->model->where('pid', 0)->orderBy('order')->get();
    // }

    public function topMenuSelectArray()
    {
    	$data = $this->model->where('pid', 0)->orderBy('order')->lists('name', 'id');

        $data = ['== 请选择 =='] + $data->toArray();

    	return $data;
    }

    // 更新排序
    public function updateSort($data)
    {
        foreach ($data as $key=>$value) {
        	$this->model->find($key)->update(['order'=>$value]);
        }

        return true;
    }  

    // 用户获得授权访问的栏目
    public function getMenuAuth()
    {
        $group = auth()->user()->group;

        // if ($group->level < 10) {
        //     return $this->model->whereIn('id', $group->menu)->orderBy('order')->get();
        // }

        return $this->model->whereIn('id', $group->menu)->orderBy('order')->get();

        // return $this->model->get();
    }  

    // 更具ID批量得到URL
    public function getUrls(array $ids)
    {
        return $this->model->whereIn('id', $ids)->where('url', '!=', '')->distinct()->lists('url');
    }
}